package com.ttt.servicer.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.ttt.mapper.DiseaseMapper;
import com.ttt.mapper.EquipmentMapper;
import com.ttt.mapper.OverviewDataMapper;
import com.ttt.pojo.Disease;
import com.ttt.pojo.Equipment;
import com.ttt.pojo.Forst;
import com.ttt.servicer.OverviewData;
import com.ttt.util.Result;
import com.ttt.util.ResultCodeEnum;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class OverviewDataImpl implements OverviewData {

    @Autowired
    private OverviewDataMapper overviewDataMapper;

    @Autowired
    private DiseaseMapper diseaseMapper;

    @Autowired
    private EquipmentMapper equipmentMapper;



    //获取林果基本信息
    @Override
    public Result findAllData() throws JsonProcessingException {
        List<Forst> list = overviewDataMapper.selectList(null);
        System.out.println("list" + list);

        //将信息转换成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        return result;
    }

    //获取林活健康的等级分布
    @Override
    public Result findHealth() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        // 设置分组，按 status 字段分组
        queryWrapper.groupBy("status");
        // 选择要查询的字段，这里查询 status 以及对应的数量（使用 MyBatis - Plus 的 count 函数 ）
        queryWrapper.select("status", "count(*) as count");

        List<Map<String, Object>> maps = overviewDataMapper.selectMaps(queryWrapper);

        //将数据转换成json
        Result result = Result.toJson(maps, ResultCodeEnum.SUCCESS);

        System.out.println("健康："+result.getData());
        return  result;

    }


    //获取总产量
    @Override
    public Result findAllYield() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        // 使用 MyBatis - Plus 的 sum 函数，对 yield 字段求和
        queryWrapper.select("sum(yield) as totalYield");
        // 执行查询，获取结果
        List<Object> list = overviewDataMapper.selectObjs(queryWrapper);
        //将数据转换成json

        Result result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("总参量："+result.getData());

        return result;
    }


    //获取平均健康指数
    @Override
    public Result findAvgHealth() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        // 使用 MyBatis - Plus 的 avg 函数，对 health 字段求平均值
        queryWrapper.select("avg(health) as avgHealth");

        List<Object> list = overviewDataMapper.selectObjs(queryWrapper);

        //将数据封装成json

        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("平均健康指数：" + result.getData()) ;

        return result;
    }

    //获取林果类型
    @Override
    public Result findType() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("type", "COUNT(*) as count")
                .groupBy("type");

        // 使用 selectMaps() 获取完整结果
        List<Map<String, Object>> list = overviewDataMapper.selectMaps(queryWrapper);
        System.out.println(list);

        // 封装数据
        Result result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("林果类型：" + result.getData());
        return result;

    }


    //获取病害数
    @Override
    public Result findDisease() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        // 使用 MyBatis - Plus 的 sum 函数，对 disease字段求和
        queryWrapper.select("sum(disease)");
        // 执行查询，获取结果
        List<Object> list = overviewDataMapper.selectObjs(queryWrapper);
        //将数据转换成json

        Result result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("已检测病害数数量："+result.getData());

        return result;

    }

   // 获取各个受灾信息-->灾害报警次数 哪个时间发生了多少次检测预警
    @Override
    public Result findDiseaseCount() throws JsonProcessingException {

        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        // 按 time 字段分组，并统计每个时间的记录数量
        queryWrapper.select("time", "count(*) as count")
                .groupBy("time");

        List<Map<String, Object>> list =diseaseMapper.selectMaps(queryWrapper);
        System.out.println(list);

        //将数据封装成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        return  result;

    }

    //获取各个受灾类型及其次数
    @Override
    public Result findDiseaseTypeCount() throws JsonProcessingException {
        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        // 按 time 字段分组，并统计每个时间的记录数量
        queryWrapper.select("type", "count(*) as count")
                .groupBy("type");

        List<Map<String, Object>> list =diseaseMapper.selectMaps(queryWrapper);
        System.out.println(list);

        //将数据封装成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        return  result;
    }

    @Override
    public Result findPositionInfo() throws JsonProcessingException {

        QueryWrapper<Disease> queryWrapper = new QueryWrapper<>();
        // 设置要查询的字段
        queryWrapper.select("name", "position", "time");
        List<Map<String, Object>> list = diseaseMapper.selectMaps(queryWrapper);

        //将数据封装成json

        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println(result.getData());

        return result;
    }


    //获取各个林果地的产量
    @Override
    public Result findYield() throws JsonProcessingException {

        QueryWrapper<Forst> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("name","yield");
        // 使用 selectMaps() 获取完整结果
        List<Map<String, Object>> list = overviewDataMapper.selectMaps(queryWrapper);
        System.out.println(list);
        // 封装数据
        Result result = Result.toJson(list, ResultCodeEnum.SUCCESS);
        System.out.println("各个林果地产量：" + result.getData());
        return result;

    }



    //获取检测点位信息
    @Override
    public Result findEquipmentInfo() throws JsonProcessingException {

        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","position","time","status");
        List<Map<String, Object>> list = equipmentMapper.selectMaps(queryWrapper);

        //将数据封装成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);

        System.out.println("检测点信息：" + result.getData());
        return result;
    }



    //获取田间管理数据

    @Override
    public Result findFieldManagerInfo() throws JsonProcessingException {

        QueryWrapper<Equipment> queryWrapper = new QueryWrapper<>();
        queryWrapper.select("id","position","time","value","content");
        List<Map<String, Object>> list = equipmentMapper.selectMaps(queryWrapper);

        //将数据封装成json
        Result<String> result = Result.toJson(list, ResultCodeEnum.SUCCESS);

        System.out.println("田间管理：" + result.getData());
        return result;


    }


}
